import java.io.*;
import java.util.*;



public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		boolean flagDeleteDB = false;
		
		Parser parser = new Parser();
		parser.parseConfig("config.txt");
		parser.parseInput("input.txt");
				
		Peer myPeer = new Peer(parser.get_firstPeer(), parser.get_firstIP(), 
				parser.get_firstTCP(),parser.get_firstUDP(), parser.get_firstPhone(), 
				parser.get_firstEmail(), parser.get_firstBuffer(), 
				parser.get_firstPicName(), parser.get_firstPicSize(), parser.get_mss(),
				parser.get_updateFrequency(), parser.get_N(),
				parser.get_peerName(), parser.get_ip(), parser.get_tcp(), parser.get_udp());
		
		Queue<String> commands = parser.get_queue();
		
		try {
			myPeer.initialize();
		} catch (IOException ex) {
			//TODO log error
			return;
		}
		
		
		
		//Executing commands
		while (!commands.isEmpty()) {
			String command = commands.poll();
			
			String[] parsed = command.split("\\&|:|\\(|,|\\$|\\)");
			
			if (parsed[0].equals("run")) { //Run command
				new Thread(myPeer).start();
				try {
					Thread.sleep(Integer.parseInt(parsed[1]) * 1000);
					myPeer.stop();
					Thread.sleep(1500);
				} catch (InterruptedException ex) {}
			} else if (parsed[0].equals("sleep")) { //Sleep command
				try {Thread.sleep(Integer.parseInt(parsed[1]) * 1000);}
				catch (InterruptedException ex) {}
			} else if (parsed[0].equals("updatedata")) { //Update owner peer data
				try {
					myPeer.UpdateDB(parsed);
				} catch (IOException ex) {
					// TODO: log error
					return;
				}
			} else if (parsed[0].equals("deletedb")) {
				if (parsed[1].equals("1")) {
					myPeer.deleteDBFile();
					flagDeleteDB = true;
					myPeer.createDB();
				}
			} else if (parsed[0].equals("SHUTDOWN")) {
				commands.clear();
				try {
					if (!flagDeleteDB)
						myPeer.saveDB();
				} catch (FileNotFoundException ex) {
					System.out.println(); //TODO log error
				} catch (IOException ex) {
					System.out.println(); //TODO log error
				}
			}
		}
		
		myPeer.stop();
		
		/*Record r1 = new Record("12/04/09;00:00:00", "12/04/09;00:00:00","Record1","10.0.0.1","2000","3467","09-7416631","mail1@try.com","02/01/09;00:00:00","1","62891","01/04/09;00:00:00");
		Record r2 = new Record("12/04/09;00:00:00", "12/04/09;00:00:00","Record2","10.0.0.1","2000","3467","09-7416632","mail2@try.com","02/01/09;00:00:00","1","775683","10/04/09;00:00:00");
		Record r3 = new Record("01/04/09;00:00:00", "01/04/09;00:00:00","Record3","10.0.0.1","2000","3467","09-7416633","mail3@try.com","02/01/09;00:00:00","1","56026","01/04/09;00:00:00");
		Record r4 = new Record("01/04/09;00:00:00", "01/04/09;00:00:00","Record4","10.0.0.1","2000","3467","09-7416634","mail4@try.com","02/01/09;00:00:00","1","164459","01/04/09;00:00:00");
		Record r5 = new Record("01/04/09;00:00:00", "01/04/09;00:00:04","Record5","10.0.0.1","2000","3467","09-7416635","Amail5@try.com","02/01/09;00:00:00","1","69264","01/04/09;00:00:00");
		
		DB db = new DB(r1.clone());
		
		db.addRecord("Record2", r2.clone());
		db.addRecord("Record3", r3.clone());
		db.addRecord("Record4", r4.clone());
		db.addRecord("Record5", r5.clone());
		
		Thread.sleep(1000);
		*/
		
		//Test1 - UDP
		//UDPTest test1 = new UDPTest();
				
		
	}

}
